function prepare_topic(d){
    function rep(i,v){
        $("#topic_"+i).html(v);
    }
    rep('id',d['id']);
    if($("a#topic-edit-button"))
        $("a#topic-edit-button").attr("href",'wizard?r=topics&tid='+d['id']);
    if($("a#topic-delete-button"))
        $("a#topic-delete-button").attr("tid",d['id']);
    rep('type',d['type']);
    rep('created',d['created']);
    rep('lastupdate',d['lastupdate']);
    rep('expires',d['expires']);
    rep('country',d['country']);
    rep('creator',d['creator']);
    rep('editor',d['editor']);
    rep('timeleft',(d['timeleft']&&d['timeleft'].length>3?d['timeleft']:'<span style="color:red">Expired!</span>'));
    rep('subject',d['subject']+(d['active'] == '1'?'':' <span style="color:red">(Disabled)</span>'));
    rep('text',d['text']);
    if(d['notes']!='') rep('notes',d['notes']);
    $("#topic_loading").hide();
    $("#topic_body").show();
}
window.topicDialog = function(tid){
    $("#dialog_topic").dialog("open");
    $("#topic_body").hide();
    $("#topic_loading").show();
    window.ajax = $.get('ajax?r=GET_TOPIC&tid='+tid,function(data){
        if((data[0]['data'] === undefined) || ($.type(data) == "string")){
                $("#dialog_topic").dialog("close");
                var data = new Array();
                data[0] = {'error': 500,'data': 'Internal Error, Please call Administrator!\n<!-- SystemErrorDetails: ' + window.data + '-->'};
            }
            $.each(data, function(index, value) {
                if(value['error']){
                    $("#dialog_topic").dialog("close");
                    console.log("Error: " + value['error'] + "\nData: " + value['data'] + "\n");
                    insertStickyError(value['data'],value['error']);
                }else{
                    prepare_topic(value['data']);
                }
            });
    });
}
$("a#topic-delete-button").click(function(){
    var tid = $(this).attr('tid');
    $("#dialog_topic").dialog("close");
    $("#dialog_delete h1#delete-header").html("Help Topic Deleting");
    $("#dialog_delete p#delete-body").html("Are you sure you want to delete Help Topic#"+tid+"<br /><i style='color:red'>"+"!!!Warning: This process can't be reverted!!!"+"</li>");
    $("#dialog_delete button.delete_confirm").attr('tid',tid);
    $("#dialog_delete").dialog("open");
});
$("button.delete_confirm").click(function(){
    var tid = $(this).attr('tid');
    $("#dialog_delete").dialog("close");
    window.ajax = $.get('ajax?r=DEL_TID&tid='+tid,function(data){
        window.data = data;
        if((data[0]['data'] === undefined) || ($.type(data) == "string")){
                var data = new Array();
                data[0] = {'error': 500,'data': 'Internal Error, Please call Administrator!\n<!-- SystemErrorDetails: ' + window.data + '-->'};
            }
        $.each(data, function(index, value) {
            if(value['error']){
                console.log("Error: " + value['error'] + "\nData: " + value['data'] + "\n");
                insertStickyError(value['data'],value['error']);
            }else{
                insertAutoSuccess(value['data']);
                $("tr[tid="+tid+"]").children('td, th')
                .animate({ padding: 0 })
                .wrapInner('<div />')
                .children()
                .slideUp(function() { $(this).closest('tr').remove(); });
            }
        });
    });
    $("#dialog_delete a#delete_confirm").attr('tid','');
})